package com.aube.interact.fighter.card;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aube.interact.fighter.R;
import com.aube.interact.fighter.model.GameConfig;
import com.aube.interact.fighter.model.OptionResultModel;
import com.aube.interact.fighter.model.ReportResultModel;
import com.aube.interact.fighter.model.TimeLinePiece;
import com.aube.interact.fighter.roundedimageview.RoundedImageView;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.model.ChannelDetail;

/**
 * Created by huyaonan on 16/11/15.
 */
public class CardInfo extends BaseCardNew {

    private boolean mBExpanded = false;

    private WebView mWebView;
    private StripeLayout mRoot;
    private ImageView mTopImage;
    private View mInfoDetailLayout, mInfoThumbnail, mCardFold;
    private RoundedImageView mInfoThumb;
    private View mClose;
    private TextView mDesc;

    private int minHeight = 0;
    private int maxHeight = 0;

    public CardInfo(Context context) {
        this(context, null);
    }

    public CardInfo(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CardInfo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        LayoutInflater.from(context).inflate(R.layout.aube_intera_card_info, this, true);

        mTopImage = (ImageView) findViewById(R.id.card_top_logo);
        mRoot = (StripeLayout) findViewById(R.id.card_content_layout);
        mWebView = (WebView) findViewById(R.id.card_info);
        mCardFold = findViewById(R.id.card_fold_layout);
        mClose = findViewById(R.id.card_close);
        mInfoDetailLayout = findViewById(R.id.card_info_detail);
        mInfoThumbnail = findViewById(R.id.card_info_thumbnail);
        mInfoThumb = (RoundedImageView) findViewById(R.id.card_info_img);
        mDesc = (TextView) findViewById(R.id.card_desp);

        mCardFold.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                switchLayout(false);
            }
        });

        mInfoThumb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                switchLayout(true);
            }
        });

        mClose.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                close();
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void init(TimeLinePiece data, ChannelDetail channelDetail) {
        super.init(data, channelDetail);

        mBExpanded = mData.isCardFold();
        ImageLoader.getInstance(getContext()).fillImage(mInfoThumb, mData.detail.pic.picurl);

        setupWebView();
    }

    private void switchLayout(final boolean showDetail) {
        if(minHeight == 0)
            minHeight = mRoot.getHeight();
        if(maxHeight == 0) {
            maxHeight = ((View) getParent()).getHeight() - mTopImage.getHeight();
        }

        if(showDetail && iSender != null) {
            iSender.addFragment(mData.getRid());

            GameConfig config = iSender.getConfig();
            //fill prizetext
            if(config != null)
                fillTextWidthDrawable(mDesc, "", "");
        }

        ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation){
                float animValue = (float) animation.getAnimatedValue();

                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mRoot.getLayoutParams();
                if(showDetail) {
                    params.height = (int) (minHeight + (maxHeight - minHeight) * animValue);
                } else {
                    params.height = (int) (maxHeight + (minHeight - maxHeight) * animValue);
                }
                mRoot.setLayoutParams(params);

                if(showDetail) {
                    mInfoDetailLayout.setAlpha(animValue);
                    mInfoThumbnail.setAlpha(1 - animValue);
                } else {
                    mInfoDetailLayout.setAlpha(1 - animValue);
                    mInfoThumbnail.setAlpha(animValue);
                }
            }
        });

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                if(showDetail) {
                    mInfoDetailLayout.setAlpha(0f);
                    mInfoDetailLayout.setVisibility(VISIBLE);
                } else {
                    mInfoThumbnail.setAlpha(0f);
                    mInfoThumbnail.setVisibility(VISIBLE);
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if(showDetail)
                    mInfoThumbnail.setVisibility(GONE);
                else
                    mInfoDetailLayout.setVisibility(GONE);
            }
        });

        animator.setDuration(400);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.start();
    }

    @Override
    public String getType() {
        return TYPE_INFO;
    }

    @Override
    public void doNext() {
    }

    @Override
    public void syncData(OptionResultModel response) {
    }

    @Override
    public boolean IsPreviewDataSynced() {
        return false;
    }

    @Override
    public void syncPreviewData(ReportResultModel result) {
    }

//    private String str = "虎扑体育11月24日讯 克里夫兰骑士队坐镇主场迎来开拓者的挑战，首节凯文-乐福早早进入状态并迎来大爆发，他在首节14投11中，三分球10投8中轰下惊人的34分，不仅刷新个人单节得分纪录，同时在联盟单节得分纪录中也仅次于克莱-汤普森的单节37分。正是凭借他的火热表现，骑士首节砍下46分并取得15分的领先，虽然乐福在第二节开始状态开始回落，但是骑士全队依旧保持着火热的状态，他们半场砍下81分，三节便拿到112分，进攻端疯狂的表现帮助他们牢守两位数的领先。最终，骑士137-125大胜开拓者拿下胜利";
    private void setupWebView() {
        String content = mData.detail.content;
        String prefix = "<html>\n" +
                "<meta name=\"viewport\" content=\"minimum-scale=1, maximum-scale=1,  initial-scale=1, user-scalable=yes, width=device-width\" />\n" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
                "<style>\n" +
                "img{\n" +
                "max-width: 100% !important;\n" +
                "height: auto !important;\n" +
                "}\n" +
                "* {\n" +
                "word-wrap: break-word;\n" +
                "word-break: break-all;\n" +
                "}\n" +
                "</style>\n" +
                "<body>\n" +
                "<font color=black>\n";
        String suffix = "\n</font>\n" +
                "</body>\n" +
                "</html>\n";

        mWebView.getSettings().setDefaultTextEncodingName("UTF-8");
        mWebView.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
        mWebView.loadData(prefix + content + suffix, "text/html; charset=UTF-8", null);
    }

}
